<template>
  <div class="school">
    <h2>学校名称：{{name}}</h2>
    <h2>地址：{{address}}</h2>
    <h2>学生姓名：{{studentName}}</h2>
  </div>
</template>

<script>
//引入发布订阅 pubsub
import pubsub from 'pubsub-js'
export default {
    name: 'School',
    data() {
        return {
            name: 'sgg',
            address: 'bj', 
            studentName: '',
        }
    },
    methods: {
    },
    mounted() {
      //订阅一个消息，把订阅id放到vc上，回调内部没有this，要用箭头函数保证this是vc
      this.pubId = pubsub.subscribe('hello', (msgName, data)=>{
        console.log('有人发布了hello消息', data)
        this.studentName = data
      })
    },
    beforeDestroy() {
      //销毁vc时取消订阅
      pubsub.unscribe(this.pubId)
    }
}
</script>

<style scoped>
  .school{
    background-color: skyblue;
    padding: 5px;
    margin-top: 20px;
  }
</style>